D:\a\scloud-dns\scloud-dns\src\workers\task.rs
Line | Count | Source |
1 | | use crate::workers::WorkerType; |
2 | | use bytes::Bytes; |
3 | | use serde::{Deserialize, Serialize}; |
4 | | use std::net::SocketAddr; |
5 | | use std::sync::Arc; |
6 | | use std::time::SystemTime; |
7 | | use tokio::sync::{OwnedSemaphorePermit, Semaphore}; |
8 | | use uuid::Uuid; |
9 | | use crate::exceptions::SCloudException; |
10 | | |
11 | | #[allow(unused)] |
12 | | #[allow(non_camel_case_types)] |
13 | | #[derive(Debug, Clone, Serialize, Deserialize)] |
14 | | pub(crate) struct SCloudWorkerTask { |
15 | | pub task_id: Uuid, |
16 | | pub for_type: WorkerType, |
17 | | pub for_who: SocketAddr, |
18 | | pub payload: Bytes, |
19 | | pub attempts: u8, |
20 | | pub max_attempts: u8, |
21 | | pub created_at: SystemTime, |
22 | | pub deadline_timeout: Option<SystemTime>, |
23 | | pub priority: u8, // if supported by the broker |
24 | | pub reply_to: Option<String>, // response endpoint |
25 | | pub correlation_id: Option<String>, // id request/response |
26 | | } |
27 | | |
28 | | pub struct InFlightTask { |
29 | | pub task: SCloudWorkerTask, |
30 | | pub _permit: OwnedSemaphorePermit, |
31 | | } |
32 | | |
33 | | impl InFlightTask { |
34 | 0 | pub async fn new( |
35 | 0 | payload: &[u8], |
36 | 0 | peer: SocketAddr, |
37 | 0 | worker_type: WorkerType, |
38 | 0 | sem: Arc<Semaphore>, |
39 | 0 | ) -> Result<Self, SCloudException> { |
40 | 0 | let permit = sem |
41 | 0 | .acquire_owned() |
42 | 0 | .await |
43 | 0 | .map_err(|_| SCloudException::SCLOUD_WORKER_SEM_CLOSED)?; |
44 | | |
45 | 0 | Ok(Self { |
46 | 0 | task: SCloudWorkerTask { |
47 | 0 | task_id: Uuid::new_v4(), |
48 | 0 | for_type: worker_type, |
49 | 0 | for_who: peer, |
50 | 0 | payload: Bytes::copy_from_slice(payload), |
51 | 0 | attempts: 0, |
52 | 0 | max_attempts: 3, |
53 | 0 | created_at: SystemTime::now(), |
54 | 0 | deadline_timeout: None, |
55 | 0 | priority: 0, |
56 | 0 | reply_to: None, |
57 | 0 | correlation_id: None, |
58 | 0 | }, |
59 | 0 | _permit: permit, |
60 | 0 | }) |
61 | 0 | } |
62 | | } |